<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>HTML DOM - Position an element absolutely to another element</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" href="/css/demo.css" />
        <link rel="preconnect" href="https://fonts.gstatic.com" />
        <link
            rel="stylesheet"
            href="https://fonts.googleapis.com/css2?family=Inter&family=Source+Code+Pro&display=swap"
        />
        <style>
            .target {
                position: relative;

                /* Center the content */
                align-items: center;
                display: flex;
                justify-content: center;

                /* Misc */
                border: 1px solid #cbd5e0;
                height: 8rem;
                width: 16rem;
            }
            .popover {
                /* Absolute position */
                left: 50%;
                position: absolute;
                top: 100%;
                transform: translate(-50%, 0.75rem);

                /* Misc */
                border: 1px solid #cbd5e0;
                padding: 0.5rem;
            }
            .arrow {
                background-color: rgb(255, 255, 255);
                border-left: 1px solid #cbd5e0;
                border-top: 1px solid #cbd5e0;
                height: 0.75rem;
                left: 50%;
                position: absolute;
                top: 0;
                transform: translate(-50%, -50%) rotate(45deg);
                width: 0.75rem;
            }
        </style>
    </head>
    <body>
        <div style="display: flex; height: 16rem; justify-content: center; padding: 1rem">
            <div>
                <div style="margin-bottom: 0.5rem; text-align: center">
                    <button
                        type="button"
                        style="
                            background-color: #3182ce;
                            border: none;
                            color: #fff;
                            cursor: pointer;
                            padding: 0.25rem 0.5rem;
                        "
                        id="toggleButton"
                    >
                        Toggle overflow:hidden
                    </button>
                </div>

                <div id="target" class="target">
                    Target

                    <div id="popover" class="popover">
                        <div class="arrow"></div>
                        Popover
                    </div>
                </div>
            </div>
        </div>

        <script>
            document.addEventListener('DOMContentLoaded', function () {
                const target = document.getElementById('target');
                const toggleButton = document.getElementById('toggleButton');

                toggleButton.addEventListener('click', function () {
                    target.style.overflow = target.style.overflow !== 'hidden' ? 'hidden' : '';
                });
            });
        </script>
    </body>
</html>
